﻿using Dapper;
using Entity;
using IRealize;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Data.SqlClient;
using Realize;
using System.Data;
using System.Threading.Tasks;

namespace WebApplication9.Controllers.HRFA
{
    public class RLBGController : Controller
    {
        private readonly IConfiguration configuration;

        public RLBGController(IConfiguration configuration)
        {
            this.configuration = configuration;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Index1()
        {
            return View();
        }


        public async Task<IActionResult> Search([FromBody] Human_File request)
        {

            IRLXLK rLXLK = new RLXLK();



            // 调用服务层执行查询
            var result = await rLXLK.SearchHumanFiles(
               configuration["SqlServer"],
                request.first_kind_name,
                request.second_kind_name,
                request.third_kind_name,
                request.human_major_kind_name,
                request.hunma_major_name,
                request.regist_time,
                request.check_time,
                request.PageNumber,
                request.PageSize

            );


            // 返回分页结果
            return Ok(new
            {
                TotalCount = result.TotalCount,
                Items = result.Items,
                PageNumber = result.PageNumber,
                PageSize = result.PageSize
            });


        }


        public IActionResult CXXS()
        {

            return View();
        }

        public IActionResult RLXG(int id)
        {
            return View();
        }




        public async Task<IActionResult> CXBG(int id)
        {
            IRLXLK rLXLK = new RLXLK();
            Human_File fileDig = await rLXLK.SelectHFIDAsync(id, configuration["SqlServer"]);


            // 映射数据为前端需要的格式，包含原机构和原职位ID
            var result = new
            {
                success = true,
                data = new
                {


                    huf_id = fileDig.huf_id,
                    human_id = fileDig.human_id,
                    human_name = fileDig.human_name,
                    human_id_card = fileDig.human_id_card,
                    first_kind_name = fileDig.first_kind_name,
                    second_kind_name = fileDig.second_kind_name,
                    third_kind_name = fileDig.third_kind_name,
                    human_major_kind_name = fileDig.human_major_kind_name,
                    hunma_major_name = fileDig.hunma_major_name,
                    human_pro_designation = fileDig.human_pro_designation,
                    human_sex = fileDig.human_sex,
                    human_email = fileDig.human_email,
                    human_mobilephone = fileDig.human_mobilephone,
                    human_qq = fileDig.human_qq,
                    human_telephone = fileDig.human_telephone,
                    human_address = fileDig.human_address,
                    human_postcode = fileDig.human_postcode,
                    human_nationality = fileDig.human_nationality,
                    human_birthplace = fileDig.human_birthplace,
                    human_birthday = fileDig.human_birthday,
                    human_race = fileDig.human_race,
                    human_religion = fileDig.human_religion,
                    human_party = fileDig.human_party,
                    human_society_security_id = fileDig.human_society_security_id,
                    human_age = fileDig.human_age,
                    human_educated_degree = fileDig.human_educated_degree,
                    human_educated_years = fileDig.human_educated_years,
                    human_educated_major = fileDig.human_educated_major,
                    salary_standard_name = fileDig.salary_standard_name,
                    human_bank = fileDig.human_bank,
                    human_account = fileDig.human_account,


                    checker = HttpContext.Session.GetString("PName"),


                    check_time = fileDig.check_time,
                    human_speciality = fileDig.human_speciality,
                    human_hobby = fileDig.human_hobby,
                    attachment_name = fileDig.attachment_name,
                    human_histroy_records = fileDig.human_histroy_records,
                    human_family_membership = fileDig.human_family_membership,
                    remark = fileDig.remark,
                    human_picture = fileDig.human_picture,



                }
            };

            return Ok(result);

        }


        public async Task<IActionResult> UpdateHR([FromBody] Human_File human_File)
        {
            IRLXLK dDGLD = new RLXLK();


            // 调用AddHF方法
            int result = await dDGLD.UpdateBGHFAsync(human_File, configuration["SqlServer"]);

            if (result > 0)
            {
                return Ok(new { success = true, message = "确认变更提交成功" });
            }
            else
            {
                return BadRequest(new { success = false, message = "确认变更提交失败" });
            }

        }




        public async Task<IActionResult> Update(int id,Human_File model,string imageFile)
        {
            model.human_picture =imageFile;
            model.check_time = DateTime.Now;

            RLXLK rLXLK = new RLXLK();
            int js = await rLXLK.UpdateHFAsync(model, configuration["SqlServer"], id);

            if (js > 0)
            {
                return RedirectToAction("CXXS");
            }
            else
            {
                return View();
            }
        }


        public IActionResult Index2()
        {
            return View();
        }

        public async Task<IActionResult> Index3(int pageSize, int currentPage)
        {
           
            string sqlserver = configuration["SqlServer"];
            using (SqlConnection conn = new SqlConnection(sqlserver))
            {
                //@pageSize, @tableName, @keyName, @currentPage, @rows
                //分页存储过程
                DynamicParameters param = new DynamicParameters();
                param.Add("@pageSize", pageSize);
                param.Add("@keyName", "huf_id");  
                param.Add("@tableName", "human_file");
                param.Add("@currentPage", currentPage);
                param.Add("@totalRows", direction: ParameterDirection.Output, dbType: DbType.Int32);
                string sql = "exec [dbo].[proc_FenYedd] @pageSize, @keyName, @tableName, @currentPage, @totalRows out";
                List<Human_File> list = conn.Query<Human_File>(sql, param).ToList();

                FenYe<Human_File> fenYecs = new FenYe<Human_File>();
                fenYecs.List = list;
                fenYecs.Rows = param.Get<int>("totalRows");
                //return fenYecs;
                return Json(fenYecs);

            }


        }


        public IActionResult RLXG1(int id) 
        {
            return View();
        }


    }
}
